﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace UCF.Core.Web.CustomControls
{
    /// <summary>
    /// 放大镜插件
    /// </summary>
    /// <example>
    /// <![CDATA[
    /// <so:JZoom runat="server" ID="aa" Selector=".jqzoom" ZoomType="innerzoom" ><a href="triumph_big1.jpg" class="jqzoom" rel='gal1'  title="triumph" ><img src="triumph_small1.jpg"  title="triumph"  style="border: 4px solid #666;"></a></so:JZoom>
    /// ]]>
    /// </example>
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:JZoom runat=server></{0}:JZoom>")]
    public class JZoom : HtmlContainerControl
    {
        /// <summary>
        /// jQuery选择器
        /// </summary>
        public string Selector { get; set; }

        /// <summary>
        /// 放大镜类型
        /// <para>standard=标准位置,innerzoom=内部放大</para>
        /// </summary>
        public string ZoomType { get; set; }

        public int ZoomWidth { get; set; }
        public int ZoomHeight { get; set; }
        public int X { get; set; }
        public int Y { get; set; }
        public string Position { get; set; }

        protected override void OnPreRender(EventArgs e)
        {
            Page.PreRenderComplete+=new EventHandler(Page_PreRenderComplete);
            base.OnPreRender(e);
        }
        private void Page_PreRenderComplete(object s, EventArgs e)
        {
            Selector = string.IsNullOrEmpty(Selector) ? ".zoom" : Selector;
            ZoomType = string.IsNullOrEmpty(ZoomType) ? "standard" : ZoomType;
            StringBuilder sb = new StringBuilder();
            if (ZoomWidth > 0) sb.AppendFormat("zoomWidth:{0},", ZoomWidth);
            if (ZoomHeight > 0) sb.AppendFormat("zoomHeight:{0},", ZoomHeight);
            if (X > 0) sb.AppendFormat("xOffset:{0},", X);
            if (Y > 0) sb.AppendFormat("yOffset:{0},", Y);
            if (!string.IsNullOrEmpty(Position)) sb.AppendFormat("position:'{0}'", Position);
            Page.RegisterHeaderLink("css-jzoom", "/DMS/Skin/Js/JqPlus/Jqzoom/jqzoom_ev/2.3/css/jquery.jqzoom.css");
            Page.ClientScript.RegisterClientScriptInclude("jzoom", "/DMS/Skin/Js/JqPlus/Jqzoom/jqzoom_ev/2.3/js/jquery.jqzoom-core-pack.js");
            Page.ClientScript.RegisterStartupScript(typeof(Page), this.UniqueID, string.Format("$(document).ready(function(){{$('{0}').jqzoom({{zoomType:'{1}',lens:true,preloadImages:false,{2}alwaysOn:false}})}});", Selector,ZoomType,sb.ToString()), true);
        }

        protected override void Render(HtmlTextWriter writer)
        {
            writer.Write(this.InnerHtml);
        }
    }
}
